package com.circle.contrller;

import com.circle.model.entity.AjaxJson;
import com.circle.model.entity.DccInternalFileEntity;
import com.circle.model.entity.DccOutFileEntity;
import com.circle.model.module.ConfigData;
import com.circle.model.module.OkPost;
import com.circle.model.module.log.LoggerABC;
import com.circle.model.module.log.LoggerBuilder;
import com.circle.model.util.ConfigDataFactory;
import com.circle.service.ExportAttachService;
import com.circle.service.ExportSpecService;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class ExportController {
    //目标：导出查询出来的内部文件的实体文件，包括章节，附件，表单
    // 1、传入id数组
    // 2、根据id，分别获取调用章节接口，和附件表单接口，获取文件的章节，附件，表单的路径数据
    // 3、解析数据，创建导出文件夹
    // 4、根据路径，将文件复制到导出文件夹中
    private ExportSpecService exportSpecService;
    private ExportAttachService exportAttachService;

    public ExportController() {
        this.exportSpecService = new ExportSpecService();
        this.exportAttachService = new ExportAttachService();
    }

    public void export(List<DccInternalFileEntity> list) {
        LoggerABC loggerABC = LoggerBuilder.build();
        ConfigData configData = ConfigDataFactory.getInstance();
        try {
            if (!configData.getExportDir().exists()) {
                FileUtils.forceMkdir(configData.getExportDir());
            }
            for (int i = 0; i < list.size(); i++) {
                DccInternalFileEntity internalFile = list.get(i);

                AjaxJson returnJson = new OkPost(configData.getUrl() + "dccSpecController.do?getPathByInternalid")
                        .addFormParam("internalid", internalFile.getId())
                        .addCookie("DCC_TOKEN", ConfigDataFactory.getInstance().getToken())
                        .execute();
                if (!returnJson.isSuccess()) {
                    continue;
                }
                File interFile = new File(configData.getExportDir(), internalFile.getSpecNo());
                if (!interFile.exists()) {
                    FileUtils.forceMkdir(interFile);
                }
                Map<String, String> dataMap = (Map<String, String>) returnJson.getObj();
                for (String key : dataMap.keySet()) {
                    String path = dataMap.get(key);
                    File srcFile = new File(path);
                    if (!srcFile.exists()) {
                        loggerABC.logMessage(LoggerABC.ERROR, "Error occurred during search: File system element for parameter 'source' does not exist: " + srcFile.getAbsolutePath());
                        continue;
                    }
                    File destFile = new File(interFile, key);
                    FileUtils.copyFile(srcFile, destFile, true); // 第三个参数为true表示复制时保留文件属性
                }
                loggerABC.logMessage(LoggerABC.DEBUG, "Exported file: " + internalFile.getSpecNo());
            }
        } catch (Exception e) {
            loggerABC.setException(e);
            loggerABC.logMessage(LoggerABC.ERROR, "Error occurred during ExportController : " + e.getMessage());
        } finally {
            loggerABC.logMessage(LoggerABC.INFO, "Export completed.");
        }
    }

    public void exportSingle(DccInternalFileEntity internalFile) {
        LoggerABC loggerABC = LoggerBuilder.build();
        ConfigData configData = ConfigDataFactory.getInstance();
        try {
            if (!configData.getExportDir().exists()) {
                FileUtils.forceMkdir(configData.getExportDir());
            }

            exportSpecService.export(internalFile, configData);
            exportAttachService.export(internalFile, configData,"13");
            exportAttachService.export(internalFile, configData,"14");

            loggerABC.logMessage(LoggerABC.DEBUG, "Exported file: " + internalFile.getSpecNo());
        } catch (Exception e) {
            loggerABC.setException(e);
            loggerABC.logMessage(LoggerABC.ERROR, "Error occurred during ExportController exportSingle: " + e.getMessage());
        } finally {
            loggerABC.logMessage(LoggerABC.INFO, "Export completed.");
        }
    }

    public void exportSingleForOut(DccOutFileEntity outFile) {
        LoggerABC loggerABC = LoggerBuilder.build();
        ConfigData configData = ConfigDataFactory.getInstance();
        try {
            if (!configData.getExportDir().exists()) {
                FileUtils.forceMkdir(configData.getExportDir());
            }

            exportAttachService.exportAttForOutFile(outFile, configData);


            loggerABC.logMessage(LoggerABC.DEBUG, "Exported Outfile: " + outFile.getSpecNo());
        } catch (Exception e) {
            loggerABC.setException(e);
            loggerABC.logMessage(LoggerABC.ERROR, "Error occurred during ExportController OutexportSingle: " + e.getMessage());
        } finally {
            loggerABC.logMessage(LoggerABC.INFO, "Export Outfilecompleted.");
        }
    }
}
